{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Printing Output to a Text File\n",
    "\n",
    "is simple. You just have to `open()` the file, write to it and then `close()` it. This notebook demonstrates this."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "printyellow (generic function with 1 method)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "using Dates\n",
    "\n",
    "include(\"printmat.jl\")   #a function for prettier matrix printing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## open"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NewTxtFile.txt has been created in the subfolder Results. It's still empty.\n"
     ]
    }
   ],
   "source": [
    "fh = open(\"Results/NewTxtFile.txt\", \"w\")        #open the file, \"w\" for writing\n",
    "\n",
    "println(\"NewTxtFile.txt has been created in the subfolder Results. It's still empty.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## write"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "println(fh,\"Dogs are \")          #printing to the file, notice the fh   \n",
    "println(fh,\"nicer than cats.\")\n",
    "println(fh,\"\\n\")\n",
    "\n",
    "x = reshape(1:20,5,4)\n",
    "printmat(fh,x,width=10,prec=0)             #to pretty print the matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## close"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NewTxtFile.txt has been closed. Check it out.\n"
     ]
    }
   ],
   "source": [
    "close(fh)                               #close the file\n",
    "\n",
    "println(\"NewTxtFile.txt has been closed. Check it out.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Julia 1.3.0",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
